<?php 

class String extends Helper {

	public static function camelToLowerWithUnderscores($str) {
        return strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $str));
    }	
	
	/**
	 * Convert a string to camel case, optionally capitalizing the first char and optionally setting which characters are
	 * acceptable.
	 *
	 * First, take existing camel case and add a space between each word so that it is in Title Form; note that
	 *   consecutive capitals (acronyms) are considered a single word.
	 * Second, capture all contigious words, capitalize the first letter and then convert the rest into lower case.
	 * Third, strip out all the non-desirable characters (i.e, non numerics).
	 *
	 * EXAMPLES:
	 * $str = 'Please_RSVP: b4 you-all arrive!';
	 *
	 * To convert a string to camel case:
	 *  strtocamel($str); // gives: PleaseRsvpB4YouAllArrive
	 *
	 * To convert a string to an acronym:
	 *  strtocamel($str, true, 'A-Z'); // gives: PRBYAA
	 *
	 * To convert a string to first-lower camel case without numerics but with underscores:
	 *  strtocamel($str, false, 'A-Za-z_'); // gives: please_RsvpBYouAllArrive
	 *
	 * @param  string  $str              text to convert to camel case.
	 * @param  bool    $capitalizeFirst  optional. whether to capitalize the first chare (e.g. "camelCase" vs. "CamelCase").
	 * @param  string  $allowed          optional. regex of the chars to allow in the final string
	 * 
	 * @return string camel cased result
	 * 
	 * @author Sean P. O. MacCath-Moran   www.emanaton.com
	 */
	static function toCamel($str, $capitalizeFirst = true, $allowed = 'A-Za-z0-9') {
		return preg_replace(
			array(
				'/([A-Z][a-z])/e', // all occurances of caps followed by lowers
				'/([a-zA-Z])([a-zA-Z]*)/e', // all occurances of words w/ first char captured separately
				'/[^'.$allowed.']+/e', // all non allowed chars (non alpha numerics, by default)
				'/^([a-zA-Z])/e' // first alpha char
			),
			array(
				'" ".$1', // add spaces
				'strtoupper("$1").strtolower("$2")', // capitalize first, lower the rest
				'', // delete undesired chars
				'strto'.($capitalizeFirst ? 'upper' : 'lower').'("$1")' // force first char to upper or lower
			),
			$str
		);
	}
	
	public static function slug($str) {
		$str = strtolower(trim($str));
		$str = preg_replace('/[^a-z0-9-]/', '-', $str);
		$str = preg_replace('/-+/', "-", $str);
		return $str;
	}
}
?>